package com.weishuang;

import com.alibaba.alink.operator.batch.BatchOperator;
import com.alibaba.alink.operator.batch.classification.LogisticRegressionPredictBatchOp;
import com.alibaba.alink.operator.batch.classification.LogisticRegressionTrainBatchOp;
import com.alibaba.alink.operator.batch.recommendation.AlsRateRecommBatchOp;
import com.alibaba.alink.operator.batch.recommendation.AlsTrainBatchOp;
import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
import org.apache.flink.types.Row;

import java.util.Arrays;
import java.util.List;

public class RecommendationDemo {
    public static void testAlsRateRecommBatchOp() throws Exception {
        List<Row> df_data = Arrays.asList(
                Row.of(1, 1, 0.6),
                Row.of(2, 2, 0.8),
                Row.of(2, 3, 0.6),
                Row.of(4, 1, 0.6),
                Row.of(4, 2, 0.3),
                Row.of(4, 3, 0.4)
        );
        BatchOperator<?> data = new MemSourceBatchOp(df_data, "user int, item int, rating double");
        BatchOperator<?> als = new AlsTrainBatchOp().setUserCol("user").setItemCol("item").setRateCol("rating")
                .setNumIter(10).setRank(10).setLambda(0.01);
        BatchOperator<?> predictor = new AlsRateRecommBatchOp()
                .setUserCol("user").setItemCol("item").setRecommCol("predicted_rating");
        BatchOperator model = als.linkFrom(data);
        predictor.linkFrom(model, data).print();
    }


    public void testAlsTrainBatchOp() throws Exception {
        List<Row> df_data = Arrays.asList(
                Row.of(1, 1, 0.6),
                Row.of(2, 2, 0.8),
                Row.of(2, 3, 0.6),
                Row.of(4, 1, 0.6),
                Row.of(4, 2, 0.3),
                Row.of(4, 3, 0.4)
        );
        BatchOperator<?> data = new MemSourceBatchOp(df_data, "user int, item int, rating double");
        BatchOperator<?> als = new AlsTrainBatchOp().setUserCol("user").setItemCol("item").setRateCol("rating")
                .setNumIter(10).setRank(10).setLambda(0.01);
        BatchOperator model = als.linkFrom(data);
        model.print();
    }

    public static void testLogisticRegressionTrainBatchOp() throws Exception {
        List <Row> df_data = Arrays.asList(
                Row.of(2, 1, 1),
                Row.of(3, 2, 1),
                Row.of(4, 3, 2),
                Row.of(2, 4, 1),
                Row.of(2, 2, 1),
                Row.of(4, 3, 2),
                Row.of(1, 2, 1),
                Row.of(5, 3, 2)
        );
        BatchOperator <?> input = new MemSourceBatchOp(df_data, "f0 int, f1 int, label int");
        BatchOperator dataTest = input;
        BatchOperator <?> lr = new LogisticRegressionTrainBatchOp().setFeatureCols("f0", "f1").setLabelCol("label");
        BatchOperator model = input.link(lr);
        BatchOperator <?> predictor = new LogisticRegressionPredictBatchOp().setPredictionCol("pred");
        predictor.linkFrom(model, dataTest).print();
    }




    public static void main(String[] args) throws Exception {
//        testAlsRateRecommBatchOp();

        testLogisticRegressionTrainBatchOp();
    }
}
